package cn.sciento.farm.devicemanager.api.jsonrpc.impl;

import cn.sciento.farm.devicemanager.api.jsonrpc.FertilizerJsonApi;
import cn.sciento.farm.devicemanager.common.constant.DeviceConstant;
import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.common.util.ReturnUtil;
import cn.sciento.farm.devicemanager.dao.entity.DevFertilizerChannel;
import cn.sciento.farm.devicemanager.dao.entity.DevWaterFertilizer;
import cn.sciento.farm.devicemanager.dao.model.FertilizerConfig;
import cn.sciento.farm.devicemanager.dao.model.ResponseMessage;
import cn.sciento.farm.devicemanager.gateway.downlink.service.MqttBlockedSendManager;
import cn.sciento.farm.devicemanager.gateway.model.downlink.fertilizer.FertilizerConfigData;
import cn.sciento.farm.devicemanager.gateway.model.downlink.fertilizer.FertilizerControlData;
import cn.sciento.farm.devicemanager.service.FertilizerChannelService;
import cn.sciento.farm.devicemanager.service.WaterFertilizerService;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.sciento.farm.devicemanager.common.util.ReturnUtil.getReturnMap;

@Service
@AutoJsonRpcServiceImpl
public class FertilizerJsonApiImpl implements FertilizerJsonApi {

    @Autowired
    private WaterFertilizerService waterFertilizerService;

    @Autowired
    private FertilizerChannelService fertilizerChannelService;

    @Autowired
    private MqttBlockedSendManager mqttBlockedSendManager;


    @Override
    public ResponseMessage create(DevWaterFertilizer waterFertilizer) {
        if (StringUtils.isEmpty(waterFertilizer.getName())) {
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        Long id = waterFertilizer.getId();
        waterFertilizer.setTenantId(null);
        if (id == null) {
            waterFertilizer.setCreateTime(System.currentTimeMillis());
            waterFertilizer.setUpdateTime(System.currentTimeMillis());
            int count = waterFertilizerService.insert(waterFertilizer);
            return getReturnMap(ResultConstant.SUCCESS, count);
        } else {
            DevWaterFertilizer oldFertilizer = waterFertilizerService.getById(id);
            if (oldFertilizer == null) {
                return getReturnMap(ResultConstant.NULL_PARAMETER);
            }
            oldFertilizer.setUpdateTime(System.currentTimeMillis());
            oldFertilizer.setLocation(waterFertilizer.getLocation());
            oldFertilizer.setModbusAddr(waterFertilizer.getModbusAddr());
            oldFertilizer.setName(waterFertilizer.getName());
            oldFertilizer.setSequence(waterFertilizer.getSequence());
            oldFertilizer.setPeriod(waterFertilizer.getPeriod());
            oldFertilizer.setRemark(waterFertilizer.getRemark());
            int count = waterFertilizerService.updateById(oldFertilizer);
            return getReturnMap(ResultConstant.SUCCESS, count);
        }
    }

    @Override
    public ResponseMessage update(DevWaterFertilizer waterFertilizer) {
        if (StringUtils.isEmpty(waterFertilizer.getName())) {
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        Long id = waterFertilizer.getId();
        waterFertilizer.setTenantId(null);
        DevWaterFertilizer oldFertilizer = waterFertilizerService.getById(id);
        if (oldFertilizer == null) {
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        oldFertilizer.setUpdateTime(System.currentTimeMillis());
        oldFertilizer.setLocation(waterFertilizer.getLocation());
        oldFertilizer.setModbusAddr(waterFertilizer.getModbusAddr());
        oldFertilizer.setName(waterFertilizer.getName());
        oldFertilizer.setSequence(waterFertilizer.getSequence());
        oldFertilizer.setPeriod(waterFertilizer.getPeriod());
        oldFertilizer.setRemark(waterFertilizer.getRemark());
        int count = waterFertilizerService.updateById(oldFertilizer);
        return getReturnMap(ResultConstant.SUCCESS, count);

    }


    @Override
    public ResponseMessage get(Long id) {
        DevWaterFertilizer waterFertilizer = waterFertilizerService.getById(id);
        return getReturnMap(ResultConstant.SUCCESS, waterFertilizer);
    }

    @Override
    public ResponseMessage delete(Long id) {
        int count = waterFertilizerService.deleteById(id);
        return getReturnMap(ResultConstant.SUCCESS, count);
    }

    @Override
    public ResponseMessage list(Integer areaId) {
        List<DevWaterFertilizer> waterFertilizerList = waterFertilizerService.getByAreaId(areaId);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS, waterFertilizerList);
    }

    @Override
    public ResponseMessage getChannelList(@PathVariable String eui) {
        List<DevFertilizerChannel> channelList = fertilizerChannelService.getChannelList(eui);
        return getReturnMap(ResultConstant.SUCCESS, channelList);
    }


    @Override
    public ResponseMessage configFertilizer(FertilizerConfig config) {
        if (config.getId() == null || config.getPeriod() == null) {
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        DevWaterFertilizer deviceWaterFertilizer = waterFertilizerService.getById(config.getId());
        if (deviceWaterFertilizer == null) {
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        FertilizerConfigData configData = new FertilizerConfigData();
        configData.setEui(deviceWaterFertilizer.getEui());
        configData.setPeriod(config.getPeriod());
        configData.setModbusAddr(deviceWaterFertilizer.getModbusAddr());
        int count = mqttBlockedSendManager.send(configData).getCode();
        deviceWaterFertilizer.setPeriod(config.getPeriod());
        waterFertilizerService.updateById(deviceWaterFertilizer);
        return getReturnMap(ResultConstant.SUCCESS, count);
    }

    @Override
    public ResponseMessage control(Long id, Integer en , String type ){
        DevFertilizerChannel fertilizerChannel = fertilizerChannelService.getById(id);
        if(fertilizerChannel == null){
            getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        DevWaterFertilizer deviceWaterFertilizer = waterFertilizerService.getByEui(fertilizerChannel.getEui());
        if(deviceWaterFertilizer == null){
            getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        FertilizerControlData controlData = new FertilizerControlData();
        switch (type){
            case DeviceConstant
                    .FERTILIZER_OPERATE_MOTOR:
                controlData.setStirringMotor(en);
                break;
            case DeviceConstant.FERTILIZER_OPERATE_VALVE:
                controlData.setFertilizeValve(en);
                break;
            default:
                return getReturnMap(ResultConstant.INVALID_PARAMETER);
        }
        controlData.setEui(deviceWaterFertilizer.getEui());
        controlData.setModbusAddr(deviceWaterFertilizer.getModbusAddr());
        controlData.setChannelNo(fertilizerChannel.getChannelNo());
        int count =  mqttBlockedSendManager.send(controlData,20 * 1000).getCode();
        return getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage setFertilizerRatio(Long id,  Float ratio ){
        DevFertilizerChannel fertilizerChannel = fertilizerChannelService.getById(id);
        if(fertilizerChannel == null){
            getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        DevWaterFertilizer deviceWaterFertilizer = waterFertilizerService.getByEui(fertilizerChannel.getEui());
        if(deviceWaterFertilizer == null){
            getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        FertilizerControlData controlData = new FertilizerControlData();
        controlData.setFertilizeRatio(ratio);
        controlData.setEui(deviceWaterFertilizer.getEui());
        controlData.setModbusAddr(deviceWaterFertilizer.getModbusAddr());
        controlData.setChannelNo(fertilizerChannel.getChannelNo());
        int count =  mqttBlockedSendManager.send(controlData,20 * 1000).getCode();
        return getReturnMap(ResultConstant.SUCCESS,count);
    }


    @Override
    public ResponseMessage controlMain(Long id, Integer time, Integer en){
        DevFertilizerChannel fertilizerChannel = fertilizerChannelService.getById(id);
        if(fertilizerChannel == null){
            getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        DevWaterFertilizer deviceWaterFertilizer = waterFertilizerService.getByEui(fertilizerChannel.getEui());
        if(deviceWaterFertilizer == null){
            getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        FertilizerControlData controlData = new FertilizerControlData();
        controlData.setEui(deviceWaterFertilizer.getEui());
        controlData.setModbusAddr(deviceWaterFertilizer.getModbusAddr());
        controlData.setChannelNo(fertilizerChannel.getChannelNo());
        controlData.setMainControl(en);
        controlData.setFertilizeTime(time);
        int count =  mqttBlockedSendManager.send(controlData).getCode();
        if(time != null){
            fertilizerChannel.setFertilizerTime(time);
            fertilizerChannelService.updateById(fertilizerChannel);
            return getReturnMap(ResultConstant.SUCCESS);
        }
        return getReturnMap(ResultConstant.SUCCESS,count);

    }

}
